package com.tools.ad.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Message;
import android.text.TextUtils;

import com.hm.ads.sdk.AdsSDKContants;
import com.hm.ads.sdk.HmAdsSDK;
import com.hm.base.android.mob.AMApplication;
import com.hm.base.android.mob.util.network.NetWorkInfoParser;
import com.tools.ad.ADModule;
import com.tools.ad.R;
import com.tools.ad.bean.AppInfo;
import com.tools.ad.log.MLog;
import com.tools.ad.util.AppUtils;


/**
 * desc: 引导所有应用外广告触发的触发器
 * date: 2017/12/11
 * author: ancun
 */

public class EngineerBroadcastReceiver extends BroadcastReceiver {
    public static final String TAG = "EngineerBroadcastReceiver";


    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }

        String action = intent.getAction();
        if (TextUtils.isEmpty(action)) {
            return;
        }

        try {
            AMApplication application = AMApplication.getInstance();
            if (application == null) {
                return;
            }
            ADModule adModule = (ADModule) application.getSubModule(ADModule.MODULE_KEY);
            if (adModule == null) {
                return;
            }

            switch (action) {
                case Intent.ACTION_PACKAGE_ADDED:
                    if (MLog.debug) {
                        MLog.e("EngineerBroadcastReceiver", "Intent.ACTION_PACKAGE_ADDED");
                    }
                    engineSmartMonitorInstall(adModule, intent);
                    break;

                case Intent.ACTION_PACKAGE_REMOVED:
                    if (MLog.debug) {
                        MLog.e("EngineerBroadcastReceiver", "Intent.ACTION_PACKAGE_REMOVED");
                    }
                    engineSmartMonitorUninstall(adModule, intent);
                    break;

                case Intent.ACTION_PACKAGE_RESTARTED:
                    if (MLog.debug) {
                        MLog.e("EngineerBroadcastReceiver", "Intent.ACTION_PACKAGE_RESTARTED");
                    }
                    //engineOuterAd(adModule);
                    break;

                case Intent.ACTION_PACKAGE_DATA_CLEARED:
                    if (MLog.debug) {
                        MLog.e("EngineerBroadcastReceiver", "Intent.ACTION_PACKAGE_DATA_CLEARED");
                    }
                    engineOuterAd(adModule, action);
                    break;

                case Intent.ACTION_USER_PRESENT:
                    if (MLog.debug) {
                        MLog.e("EngineerBroadcastReceiver", "Intent.ACTION_USER_PRESENT");
                    }
                    engineOuterAd(adModule, action);
                    break;

                case Intent.ACTION_SCREEN_ON:
                    if (MLog.debug) {
                        MLog.e("EngineerBroadcastReceiver", "Intent.ACTION_SCREEN_ON");
                    }
                    //engineOuterAd(adModule);
                    break;

                case Intent.ACTION_SCREEN_OFF:
                    //锁屏时，每隔两个小时更新广告配置
                    long current = System.currentTimeMillis();
                    if (NetWorkInfoParser.isNetRawConnect() && current - HmAdsSDK.getInstance().getLastConfigUpdateTime() > 2 * 3600 * 1000L) {
                        HmAdsSDK.getInstance().updateAdConfig();
                    }
                    if (MLog.debug) {
                        MLog.e("EngineerBroadcastReceiver", "Intent.ACTION_SCREEN_OFF");
                    }
                    break;

                case ConnectivityManager.CONNECTIVITY_ACTION:
                    if (MLog.debug) {
                        MLog.e("EngineerBroadcastReceiver", "Intent.CONNECTIVITY_ACTION");
                    }
                    engineOuterAd(adModule, action);
                    break;

                case AdsSDKContants.DEAMON_SERVICE_ALARM:
                    if (MLog.debug) {
                        MLog.e("EngineerBroadcastReceiver", "alarm_receiver");
                    }
                    engineOuterAd(adModule, action);
                    break;

                default:
                    break;
            }
        } catch (Exception ex) {
            if (MLog.debug) {
                ex.printStackTrace();
            }
        }

    }

    /**
     * 触发智能监控卸载
     *
     * @param adModule
     * @param intent
     */
    private void engineSmartMonitorUninstall(ADModule adModule, Intent intent) {
        Uri uri = intent.getData();
        if (uri == null) {
            return;
        }

        String packageName = intent.getData().getSchemeSpecificPart();
        if (packageName != null && !AMApplication.getInstance().getPackageName().equals(packageName)) {
            AppInfo appInfo = new AppInfo(packageName, intent.getAction());
            Message message = Message.obtain();
            message.what = R.id.msg_ad_engine_outer_ad;
            message.obj = appInfo;
            adModule.sendMessage(message);
        }
    }

    /**
     * 触发智能监控安装
     *
     * @param adModule
     * @param intent
     */
    private void engineSmartMonitorInstall(ADModule adModule, Intent intent) {
        Uri uri = intent.getData();
        if (uri == null) {
            return;
        }

        String packageName = intent.getData().getSchemeSpecificPart();
        if (packageName != null && !AMApplication.getInstance().getPackageName().equals(packageName)) {
            String appName = AppUtils.getAppName(AMApplication.getInstance(), packageName);
            Drawable appIcon = AppUtils.getAppIcon(AMApplication.getInstance(), packageName);
            long appSize = AppUtils.getAppSize(AMApplication.getInstance(), packageName);
            AppInfo appInfo = new AppInfo(packageName, appName, appIcon, appSize, intent.getAction());

            Message message = Message.obtain();
            message.what = R.id.msg_ad_engine_outer_ad;
            message.obj = appInfo;
            adModule.sendMessage(message);
        }
    }

    /**
     * 触发应用外广告 和 下拉环
     *
     * @param adModule
     * @param action
     */
    private void engineOuterAd(ADModule adModule, String action) {
        Message message = Message.obtain();
        message.what = R.id.msg_ad_engine_outer_ad;
        message.obj = action;
        adModule.sendMessage(message);
    }

}
